package com.shine.icis.action;

import com.shine.cotrun.web.comm.ListPage;
import com.shine.esim.struts.webutil.SessionParam;
import com.shine.icis.blo.IDataSubmitService;
import com.shine.icis.blo.ISubmitLogService;
import com.shine.icis.config.SysConstants;
import com.shine.icis.dao.param.SubmitLogQueryParameters;
import com.shine.icis.exception.ICISException;
import com.shine.icis.hbean.SubmitLog;
import com.shine.icis.hbean.SubmitReport;
import com.shine.icis.util.EFRameInf;

public class SubmitLogAction extends BaseAction {

	/**
	 * 生成文件查询
	 */
	private static final long serialVersionUID = 7638121893867812633L;
	
	private ISubmitLogService submitLogService;
	private IDataSubmitService dataSubmitService;
	
	// input param
	private int pageNo;
	private int pageSize;
	private String beginDataDate;
	private String endDataDate;
	private String logId;//报送日志ID
	
	//output value
	private ListPage<SubmitLog> submitLogPage;
	private SubmitReport submitReport;
	private String downloadUrl;
	private String currentBusiDate;//当前业务日期
	
	public void setSubmitLogService(ISubmitLogService submitLogService) {
		this.submitLogService = submitLogService;
	}
	
	public void setDataSubmitService(IDataSubmitService dataSubmitService) {
		this.dataSubmitService = dataSubmitService;
	}
	
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	public void setBeginDataDate(String beginDataDate) {
		this.beginDataDate = beginDataDate;
	}

	public void setEndDataDate(String endDataDate) {
		this.endDataDate = endDataDate;
	}

	public void setLogId(String logId) {
		this.logId = logId;
	}

	public ListPage<SubmitLog> getSubmitLogPage() {
		return submitLogPage;
	}

	public SubmitReport getSubmitReport() {
		return submitReport;
	}

	public String getDownloadUrl() {
		return downloadUrl;
	}

	public String getCurrentBusiDate() {
		return currentBusiDate;
	}
	
	/**
	 * 加载报送配置
	 * @return
	 */
	public String initQuerySubmitLog() {
		currentBusiDate = dataSubmitService.getBusiDate();
		return success();
	}
	
	public String querySubmitLog() {
		SubmitLogQueryParameters pqp = new SubmitLogQueryParameters();
		
		//设置分页信息
		pqp.setPageNo(pageNo);
		pqp.setPageSize(pageSize);
		//排序信息
		pqp.addOrder("operateTime", false);
		pqp.addOrder("id", true);
		//参数
		if( beginDataDate != null && !beginDataDate.trim().equals("") ){
			pqp.setBeginDataDate(beginDataDate.trim().replaceAll("-", ""));
		}
		if( endDataDate != null && !endDataDate.trim().equals("") ){
			pqp.setEndDataDate(endDataDate.trim().replaceAll("-", ""));
		}
		try {
			submitLogPage = submitLogService.getSubmitLogPage(pqp);
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		}
	}
	
	public String viewSubmitReport() {
		if (logId == null || logId.trim().equals("")) {
			return error("参数不能为空");
		}
		try {
			submitReport = submitLogService.getSubmitReportBySubmitLogId(logId);
			if (submitReport != null) {
				submitReport.setSubmitLog(submitLogService.getSubmitLogById(submitReport.getSubmitLogId()));
			}
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		}
	}
	
	public String initDownloadSubmitLog() {
		currentBusiDate = dataSubmitService.getBusiDate();
		return success();
	}

	public String downloadSubmitLog() {
		if (logId == null || logId.trim().equals("")) {
			return error("参数不能为空");
		}
		boolean success = false;
		try {
			downloadUrl = "/" + submitLogService.csCompressSubmitFiles(logId);
			success = true;
			return success();
		} catch(ICISException e) {
			return error(e.getMessage());
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		} finally {
			SessionParam user = (SessionParam)getSession().getAttribute(SysConstants.SESSION_USER_KEY);
			//记录系统日志
			EFRameInf.saveOpLog(user.getUSER_CODE(), EFRameInf.FUNCCODE_FILEDOWNLOAD, "生成文件下载", success);
		}
	}
}
